<?php
// +----------------------------------------------------------------------
// | INPHP
// | Copyright (c) 2023 https://inphp.cc All rights reserved.
// | Author: 幺月儿(https://gitee.com/lulanyin) Email: inphp@qq.com
// | 该文件的开源协议以所在项目的LICENSE文件为准，请遵守开源协议要求
// +----------------------------------------------------------------------
// | 模板管理
// +----------------------------------------------------------------------
namespace app\cms\http\api;

use app\admin\attributes\auth;
use Inphp\Core\Modules;
use Inphp\Core\Util\File;

#[auth("cms/template")] class template
{
    public function list(): array
    {
        $root = Modules::getModule()->root;
        $list = [];
        //读取
        $dirList = File::getDirs($root."/template");
        foreach ($dirList as $dir) {
            if (is_file($dir["path"]."/config.json") && is_file($dir["path"]."/screenshot.jpg")) {
                $config = file_get_contents($dir["path"]."/config.json");
                $config = @json_decode($config, true) ?? [];
                if (isset($config["name"])) {
                    $list[] = [
                        "id"        => $dir["name"],
                        "name"      => $config["name"],
                        //使用通用资源获取链接
                        "screenshot"=> assetsUrl("/template/{$dir['name']}/screenshot.jpg", "cms")
                    ];
                }
            }
        }
        return $list;
    }
}